package cn.stylefeng.guns.modular.ry600.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.stylefeng.roses.core.base.controller.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.util.ServerResponse;

import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.stylefeng.guns.modular.system.model.Bindclient;
import cn.stylefeng.guns.modular.system.model.Clientcustomer;
import cn.stylefeng.guns.modular.system.model.ErpProduct;
import cn.stylefeng.guns.modular.system.model.Erpkind;
import cn.stylefeng.guns.modular.ry600.service.IBindclientService;
import cn.stylefeng.guns.modular.ry600.service.IErpkindService;

/**
 * 连锁绑定门店管理控制器
 *
 * @author fengshuonan
 * @Date 2018-12-24 10:43:00
 */
@Controller
@RequestMapping("/bindclient")
public class BindclientController extends BaseController {

    private String PREFIX = "/ry600/bindclient/";

    @Autowired
    private IBindclientService bindclientService;
    @Autowired
    private IErpkindService erpkindService;

    /**
     * 跳转到连锁绑定门店管理首页
     */
    @RequestMapping("")
    public String index(@RequestParam(required = false) String UorgId,HttpServletRequest request) {
    	if(!StringUtils.isBlank(UorgId)){
    		request.setAttribute("orgId", UorgId);
    		
    	}else{
    		request.setAttribute("orgId", "UorgId");
    	}
        return PREFIX + "bindclient.html";
    }

    /**
     * 跳转到添加连锁绑定门店管理
     */
    @RequestMapping("/bindclient_add")
    public String bindclientAdd() {
        return PREFIX + "bindclient_add.html";
    }

    /**
     * 跳转到修改连锁绑定门店管理
     */
    @RequestMapping("/bindclient_update/{bindclientId}")
    public String bindclientUpdate(@PathVariable Integer bindclientId, Model model) {
        Bindclient bindclient = bindclientService.selectById(bindclientId);
        model.addAttribute("item",bindclient);
        LogObjectHolder.me().set(bindclient);
        return PREFIX + "bindclient_edit.html";
    }

    /**
     * 获取连锁绑定门店管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return bindclientService.selectList(null);
    }

    /**
     * 新增连锁绑定门店管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Bindclient bindclient) {
        bindclientService.insert(bindclient);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/delByDao")
    @ResponseBody
    public Object delByDao(@RequestParam String clientId,@RequestParam String bindclientId) {
    	Bindclient bindclient=new Bindclient();
    	bindclient.setClientId(clientId);
    	bindclient.setBindclientId(bindclientId);
    	List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				bindclientService.delByDao(bindclient, resultErpKind.get(j).getDataSource());
			}
			
		}
        return SUCCESS_TIP;
    }
    
    
    @RequestMapping(value = "/delByDaos")
    @ResponseBody
    public Object delByDaos(@RequestParam String json) {
    	List<Bindclient> bindclients = JSON.parseArray(json,
				Bindclient.class);
    	List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				bindclientService.delByDaos(bindclients, resultErpKind.get(j).getDataSource());
			}
			
		}
        return SUCCESS_TIP;
    }
    
    
    @RequestMapping(value = "/deletes")
    @ResponseBody
    public Object deletes(@RequestParam Integer bindclientId) {
    	List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				bindclientService.deletes(bindclientId, resultErpKind.get(j).getDataSource());
			}
		}
        return SUCCESS_TIP;
    }
    /**
     * 删除连锁绑定门店管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer bindclientId) {
        bindclientService.deleteById(bindclientId);
        return SUCCESS_TIP;
    }

    /**
     * 修改连锁绑定门店管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Bindclient bindclient) {
        bindclientService.updateById(bindclient);
        return SUCCESS_TIP;
    }

    /**
     * 连锁绑定门店管理详情
     */
    @RequestMapping(value = "/detail/{bindclientId}")
    @ResponseBody
    public Object detail(@PathVariable("bindclientId") Integer bindclientId) {
        return bindclientService.selectById(bindclientId);
    }
    
    @RequestMapping(value = "/selectByMsg")
    @ResponseBody
    public Object selectByMsg(HttpServletRequest request, HttpServletResponse response) {
    	
    	if(!StringUtils.isBlank(request.getParameter("page")) && !StringUtils.isBlank(request.getParameter("rows"))){
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer rows = Integer.valueOf(request.getParameter("rows"));
			Map<String, Object> msg=this.setMap(request);
			PageHelper.startPage(page, rows);
	    	List<Bindclient> result=(List<Bindclient>) bindclientService.selectByMsg(msg);
	    	if(result.size()>0){
	    		PageInfo<Bindclient> pageInfo = new PageInfo<Bindclient>(result);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("rows", pageInfo.getList());
				map.put("total", pageInfo.getTotal());
				return map;
	    	}else{
	    		return result;
	    	}
	    	
    	}
    	Map<String, Object> msg=this.setMap(request);
    	Object result=bindclientService.selectByMsg(msg);
    	return result;
        
    }
    
    public Map<String,Object> setMap(HttpServletRequest request){
    	Map<String, Object> msg = new HashMap<String, Object>();
    	String ifBind=request.getParameter("ifBind");
    	String dataSource=request.getParameter("dataSource");
    	String searchKey = request.getParameter("searchKey");
		String searchWord = request.getParameter("searchWord");
		if (!StringUtils.isBlank(ifBind)) {
			msg.put("ifBind", ifBind.trim());
		}
		if (!StringUtils.isBlank(dataSource)) {
			List<Erpkind> resultErpKind=erpkindService.selectList(null);
			for(int i=0;i<resultErpKind.size();i++){
	    		if(dataSource.equals(resultErpKind.get(i).getAccountId())){
	    			msg.put("dataSource", resultErpKind.get(i).getDataSource());
	    			}
	    		}
		}
		if (!StringUtils.isBlank(searchKey) && !(StringUtils.isBlank(searchWord)||"UorgId".equals(searchWord))) {
			msg.put(searchKey.trim(), searchWord.trim());
		}
		return msg;
    }
    
    
    @RequestMapping(value = "/batchadd")
	@ResponseBody
	public Object batchadd(String json) {
		// List<Clientcustomer> clientcustomers=(List<Clientcustomer>)
		// JSON.parse(json);
		List<Bindclient> bindclients = JSON.parseArray(json,
				Bindclient.class);
		if(bindclients.size()>1){
			return ServerResponse.createByErrorMessage("绑定条目数过多");
		}
//		if(bindclients.get(0).getClientId().equals(bindclients.get(0).getBindclientId())){
//			return ServerResponse.createByErrorMessage("绑定条目数过多");
//		}
		List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				for(int i=0;i<bindclients.size();i++){
					if(bindclients.get(i).getBindclientId().substring(0, 1).equals(bindclients.get(i).getClientId().substring(0, 1))){
						return ServerResponse.createByErrorMessage("不可绑定同一数据库的客户信息");
					}
					
					bindclientService.insByDao(bindclients.get(i),resultErpKind.get(j).getDataSource());
				}
			}
		}
		return ServerResponse.createBySuccessMessage("添加成功");
	}
    
}
